package com.canbot.u05.activity.training;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.glide.GlideApp;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.view.TrainingEditTextDialog;


/**
 * 培训主持模式------图片设置界面
 */

public class TrainingHostImageSettingActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private String isUpdate;

        private String mPicPath;

        private RelativeLayout relAdd;

        private String actionId;

        private String path;

        private ImageView imageShow;

        private TrainingEditTextDialog editTextDialog;

        private String taskName;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_training_host_image_setting);
                initView();
                isUpdate = getIntent().getStringExtra("isUpdate");
                taskName = getIntent().getStringExtra("taskName");
                if(!TextUtils.isEmpty(getIntent().getStringExtra("isFirst")) && TextUtils.isEmpty(isUpdate)){
                        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.TRAINING_DIR + "rwbzbzts_pxzc_u05_001.wav");
                        MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "18");
                }
        }

        private void initView() {
                findViewById(R.id.bt_back).setOnClickListener(this);
                findViewById(R.id.bt_finish).setOnClickListener(this);
                imageShow = (ImageView) findViewById(R.id.image_show);
                imageShow.setOnClickListener(this);
                relAdd = (RelativeLayout) findViewById(R.id.rel_add);
                relAdd.setOnClickListener(this);
        }

        @Override
        protected void onResume() {
                super.onResume();
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.bt_back:

                                if (TextUtils.isEmpty(mPicPath) && TextUtils.isEmpty(isUpdate)) {
                                        editTextDialog = new TrainingEditTextDialog(TrainingHostImageSettingActivity.this, 2);
                                        editTextDialog.setTitle("退出后将清空当前数据", "是否确定退出");
                                        editTextDialog.setOnNegativeListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                        editTextDialog.dismiss();
                                                }
                                        });
                                        editTextDialog.setOnPositiveListener(new View.OnClickListener() {
                                                @Override
                                                public void onClick(View view) {
                                                        editTextDialog.dismiss();
                                                        finish();
                                                }
                                        });
                                        editTextDialog.show();
                                        return;
                                }
                                finish();
                                break;
                        case R.id.bt_finish:

                                if(TextUtils.isEmpty(mPicPath)){
                                        Toast.makeText(this, "请选择图片", Toast.LENGTH_SHORT).show();
                                        return;
                                }

                                if (!TextUtils.isEmpty(isUpdate)) {
                                        setResult(RESULT_OK, new Intent()
                                                .putExtra("mPicPath", mPicPath));
                                        finish();
                                        return;
                                }
                                if (TextUtils.isEmpty(isUpdate)) {
                                        Intent intent = new Intent(TrainingHostImageSettingActivity.this, TrainingAudioSettingActivity.class);
                                        intent.putExtra("mPicPath",mPicPath);
                                        intent.putExtra("taskName",taskName);
                                        intent.putExtra("isFirst", getIntent().getStringExtra("isFirst"));
                                        startActivityForResult(intent, 110);
                                        overridePendingTransition(R.anim.face_zoom_enter,
                                                R.anim.face_zoom_exit);
                                        return;
                                }
                                finish();
                                break;
                        case R.id.rel_add:
                        case R.id.image_show:
                                Intent intent = new Intent(TrainingHostImageSettingActivity.this, TrainingChooseImageActivity.class);
                                intent.putExtra("mPicPath",mPicPath);
                                Logger.e("picPaths mPicPath",mPicPath);
                                intent.putExtra("oldPicPath",getIntent().getStringExtra("oldPicPath"));
                                startActivityForResult(intent, 100);
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                }
        }


        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (data == null) {
                        return;
                }
                if (requestCode == 100 && resultCode == RESULT_OK) {
                        mPicPath = data.getStringExtra("mPicPath");
                        Logger.e("picPaths mPicPath",mPicPath);
                        relAdd.setVisibility(View.GONE);
                        GlideApp.with(this).asDrawable()
                                .load(IpMsgConst.HTTP_URL_CHEST +mPicPath.replace("/uupic/", "/uupic_thumb/"))
                                .thumbnail(0.1f)
                                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                                .skipMemoryCache(false)
                                .placeholder(R.drawable.sales_banner01)//占位图
                                .error(R.drawable.sales_banner01)
                                .into(imageShow);
                        return;
                }

                if (requestCode == 110 && resultCode == RESULT_OK) {

                        path = data.getStringExtra("path");
                        actionId = data.getStringExtra("actionId");
                        Logger.e("TrainingHostImageSettingActivity", actionId + "actionId----" + path + "path----" );

                        if (TextUtils.isEmpty(path) || TextUtils.isEmpty(actionId)) {

                                editTextDialog = new TrainingEditTextDialog(TrainingHostImageSettingActivity.this, 2);
                                editTextDialog.setTitle("退出后将清空当前数据", "是否确定退出");
                                editTextDialog.setOnNegativeListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                                editTextDialog.dismiss();
                                        }
                                });
                                editTextDialog.setOnPositiveListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                                editTextDialog.dismiss();
                                                finish();
                                        }
                                });
                                editTextDialog.show();
                        }
                }
        }
}
